package com.koron.car.web.applycar.controller;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.koron.bean.base.Response;
import com.koron.bean.system.staff.vo.EamUser;
import com.koron.car.web.applycar.bean.entity.DriverRecord;
import com.koron.car.web.applycar.bean.query.DriverRecordQuery;
import com.koron.car.web.applycar.bean.query.StatisticalReportQuery;
import com.koron.car.web.applycar.bean.vo.CarStatisticalReportVO;
import com.koron.car.web.applycar.bean.vo.DriverRecordVO;
import com.koron.car.web.applycar.bean.vo.DriverStatisticalReportVO;
import com.koron.car.web.applycar.service.DriverRecordService;
import com.koron.common.core.annocation.CasUser;
import com.koron.common.core.config.ThreadLocalContext;
import com.koron.common.core.interceptor.OrderByInterceptor;
import com.koron.common.core.util.ExportExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.koron.ebs.mybatis.ADOConnection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @author twcao
 * @title: DriverRecordController
 * @projectName GDH_EAM_V2
 * @description: 行驶记录
 * @date 2022/3/918:38
 */
@Slf4j
@RestController
@RequestMapping("/driver/record")
@Api(tags = "车辆管理-行驶记录")
public class DriverRecordController {

    @Autowired
    private DriverRecordService recordService;

    @PostMapping("/saveOrUpdate")
    @ApiOperation("创建/编辑行驶记录")
    public Response<DriverRecord> create(@RequestBody DriverRecord record) {
        log.info("创建/编辑行驶记录， record = {}", JSON.toJSONString(record));
        DriverRecord bean = ADOConnection.runTask(ThreadLocalContext.get().getCurrDs(), recordService, StringUtils.isBlank(record.getId()) ? "insert" : "update", DriverRecord.class, record);
        return Response.ok(bean);
    }

    @GetMapping("/getByApplyId")
    @ApiOperation("根据申请id查出所有的行驶记录, 根据创建时间倒序")
    public Response<List<DriverRecord>> getByApplyId(String applyId) {
        log.info("根据申请id查出所有的行驶记录， applyId = {}", applyId);
        List<DriverRecord> beans = ADOConnection.runTask(ThreadLocalContext.get().getCurrDs(), recordService, "getByApplyId", List.class, applyId);
        return Response.ok(beans);
    }

    @GetMapping("/deleteById/{id}")
    @ApiOperation("根据id删除对应的行驶记录")
    public Response<Boolean> deleteById(@PathVariable("id") String id) {
        log.info("根据id删除对应的行驶记录， id = {}", id);
        Boolean flag = ADOConnection.runTask(ThreadLocalContext.get().getCurrDs(), recordService, "deleteById", Boolean.class, id);
        return Response.ok(flag);
    }

    @GetMapping("/queryList")
    @ApiOperation("行驶记录列表查询")
    public Response<List<DriverRecordVO>> queryList(DriverRecordQuery query) {
        log.info("行驶记录列表查询， query = {}", query);
        OrderByInterceptor.setOrderBy(query.getOrders());
        PageHelper.startPage(query.getPage(), query.getPageSize());
        List<DriverRecordVO> beans = ADOConnection.runTask(ThreadLocalContext.get().getCurrDs(), recordService, "queryList", List.class, query);
        return Response.ok(beans);
    }

    @PostMapping("/queryDriverStaList")
    @ApiOperation("车队司机月度统计信息")
    public Response<PageInfo> queryDriverStaList(@RequestBody StatisticalReportQuery query) {
        log.info("车队司机月度统计信息，query = {}", query);
        OrderByInterceptor.setOrderBy(query.getOrders());
        PageHelper.startPage(query.getPage(), query.getPageSize());
        List<DriverStatisticalReportVO> beans = ADOConnection.runTask(ThreadLocalContext.get().getCurrDs(), recordService, "queryDriverStaList", List.class,query);
        return Response.ok(new PageInfo<>(beans));
    }


    @GetMapping("/downDriverStaList")
    @ApiOperation("下载车队司机月度统计信息")
    public void downDriverStaList(StatisticalReportQuery query, HttpServletResponse response) {
        log.info("下载车队司机月度统计信息，StatisticalReportQuery = {}", query);
        List<DriverStatisticalReportVO> beans = ADOConnection.runTask(ThreadLocalContext.get().getCurrDs(), recordService, "queryDriverStaList", List.class, query);
        String title = query == null ? "司机月度统计信息" : query.getStatisticalBeginMonth()+"到"+query.getStatisticalEndMonth() + "月份司机月度统计信息";
        ExportExcelUtil.exportExcel(beans, title, title, DriverStatisticalReportVO.class, "司机月度统计信息", response);
    }

    @PostMapping("/queryCarStaList")
    @ApiOperation("查询车辆行程管理信息")
    public Response<PageInfo> queryCarStaList(@RequestBody StatisticalReportQuery query) {
        log.info("查询车辆行程管理信息，query = {}", query);
        OrderByInterceptor.setOrderBy(query.getOrders());
        PageHelper.startPage(query.getPage(), query.getPageSize());
        List<CarStatisticalReportVO> beans = ADOConnection.runTask(ThreadLocalContext.get().getCurrDs(), recordService, "queryCarStaList", List.class, query);
        return Response.ok(new PageInfo<>(beans));
    }

    @GetMapping("/downCarStaList")
    @ApiOperation("下载车辆行程管理信息")
    public void downCarStaList(StatisticalReportQuery query, HttpServletResponse response) {
        log.info("下载车辆行程管理信息，StatisticalReportQuery = {}", query);
        ADOConnection.runTask(ThreadLocalContext.get().getCurrDs(), recordService, "calCarNum", query, response);
    }

    @GetMapping("/submitDriverRecordFee")
    @ApiOperation("提交行驶记录费用")
    @ApiImplicitParam(name = "applyId", value = "申请单id", required = true)
    public Response<String> submitDriverRecordFee(@CasUser @ApiIgnore EamUser user, @RequestParam String applyId) {
        ADOConnection.runTask(user.getCurrDs(), recordService, "submitDriverRecordFee", applyId);
        return Response.ok();
    }
}
